vec2 fixuv(in vec2 uv) {
    vec2 p;

    float f = min(uv.x / iResolution.x, uv.y / iResolution.y);

    float m = min(iResolution.x, iResolution.y);

    p.x = uv.x / m;
    p.y = 1. - uv.y / m;

    p *= 12.;
    return p;
}

vec3 drawPoint(in vec2 p, in vec2 pos, in float s, in vec3 color, in vec3 col) {

    col = mix(col, color, smoothstep(s, s * 0.9, length(p - pos)));
    return col;
}

vec3 drawGrid(in vec2 p) {

    float f = floor(p.x) + floor(p.y);
    vec3 col = mix(vec3(0.5), vec3(0.85), smoothstep(0., 1., mod(f, 2.)));
    col = mix(col, vec3(1., 0., 0.), smoothstep(abs(p.x), abs(p.x), fwidth(p.x) * 2.));
    col = mix(col, vec3(0., 0., 1.), smoothstep(abs(p.y), abs(p.y), fwidth(p.y) * 2.));
    return col;
}

vec2 eval(in vec2 p, in vec2 sp, in vec2 ep, in float d, in float s) {

    vec2 v = ep - sp;
    vec2 a = p - sp;
    // vec2 b = a - v * clamp(dot(a, v) / dot(v, v), 0.0, 1.0);
    vec2 b = a - v * clamp(dot(a, v) / pow(length(v), 2.), 0.0, 1.0);
    d = min(d, dot(b, b));
    bvec3 cond = bvec3(p.y >= sp.y, p.y < ep.y, v.x * a.y > v.y * a.x);

    if(all(cond) || all(not(cond)))
        s = -s;

    return vec2(s, d);
}

float sdPolygon(in vec2 p, in vec2[4] v) {

    const int num = v.length();

    vec2 lt = v[0];
    vec2 rt = v[1];
    vec2 rb = v[2];
    vec2 lb = v[3];
    // 当前点到左上角点的 模长平方
    float d = dot(p - lt, p - lt);
    float s = 1.0;

    vec2 res = eval(p, lt, lb, d, s);
    s = res.x;
    d = res.y;
    res = eval(p, rt, lt, d, s);
    s = res.x;
    d = res.y;
    res = eval(p, rb, rt, d, s);
    s = res.x;
    d = res.y;
    res = eval(p, lb, rb, d, s);
    s = res.x;
    d = res.y;
    return s * sqrt(d);

}

vec3 drawPoints(in vec2 p, in vec2[4] v, in vec3 col) {
    for(int i = 0; i < v.length(); i++) {
        vec2 pos = v[i];
        col = drawPoint(p, pos, fwidth(p.y) * 5., vec3(1., 0., 0.), col);
    }
    return col;
}

/*
*绘制线段
*
*/
vec3 drawSegment(in vec2 p, in vec2 sp, in vec2 ep, in float w, in vec3 color, in vec3 col) {

    vec2 v = ep - sp;
    vec2 v1 = sp - ep;
    vec2 a = p - sp;
    vec2 a1 = p - ep;
    //计算夹角
    float theta = dot(v, a) / pow(length(v), 2.);
    float d = length(v) * theta;
    float b = sqrt(pow(length(a), 2.) - pow(d, 2.));
    if(dot(v, a) > 0.0 && dot(v1, a1) > 0.) {
        col = mix(col, color, smoothstep(w, w * 0.99, b));
    }
    return col;
}

vec3 drawAngleSegment(in vec2 p, in vec2 sp, in float angle,in float len, in float w, in vec3 color, in vec3 col) {

    //角度转弧度
    float radin = 3.1415/180.*angle;
    vec2 ep  =sp+ (vec2(cos(radin),sin(radin))*len);

    col = drawSegment(p,sp,ep,w,color,col);

    return col;
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
    vec2 p = fixuv(fragCoord);
    vec2 m = fixuv(iMouse.xy);

    vec3 col = drawGrid(p);
    // vec3 col = vec3(0.);

    vec2 lt = (vec2(4.5, 3.5));
    vec2 rt = (vec2(5.0, 0.5));
    vec2 rb = (vec2(5.0, 4.));
    vec2 lb = (vec2(1.0, 4.));

    // add more points
    vec2[] polygon = vec2[](lt, rt, rb, lb);
    float d = sdPolygon(p, polygon);
    col = mix(col, vec3(1., 0.5, 0.6), 1. - smoothstep(0., .012, d));

    // col = drawPoint(p, vec2(1., 1.), fwidth(p.y) * 5., vec3(1., 0., 0.), col);

    col = drawPoint(p, lt, fwidth(p.y) * 5., vec3(1., 08., 0.), col);
    col = drawPoint(p, rt, fwidth(p.y) * 5., vec3(1., 0., 02.), col);
    col = drawPoint(p, lb, fwidth(p.y) * 5., vec3(1., 0.1, 0.6), col);
    col = drawPoint(p, rb, fwidth(p.y) * 5., vec3(0.5, 0.4, 0.9), col);

    if(iMouse.z > 0.001) {
        d = sdPolygon(m, polygon);
        col = mix(col, vec3(1.0, 1.0, 0.0), 1.0 - smoothstep(0.0, 0.005, abs(length(p - m) - abs(d)) - 0.0025));
        col = mix(col, vec3(1.0, 1.0, 0.0), 1.0 - smoothstep(0.0, 0.005, length(p - m) - 0.015));
    }

    col = drawSegment(p, vec2(1., 5.), vec2(4., 8.), fwidth(p.y) * 100., vec3(0, 0.47, 1.), col);
    col = drawAngleSegment(p,vec2(8.,5.),iTime*100.+45.,2.+abs(cos(iTime))*4.5,fwidth(p.y)*10.,vec3(0.7,0.0,0.7),col);
    fragColor = vec4(col, 1.0);
}